package com.xw.xaudioplayer;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

import com.sobey.xaudioplayer.databinding.ActivityMainBinding;
import com.xw.musicplayer.bean.Music;
import com.xw.musicplayer.MusicPlayer;
import com.xw.musicplayer.listener.OnBufferingListener;
import com.xw.musicplayer.listener.OnMusicChangeListener;
import com.xw.musicplayer.listener.OnMusicClickListener;
import com.xw.musicplayer.listener.OnProgressListener;
import com.xw.musicplayer.listener.OnStatusChangeListener;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements OnBufferingListener, OnMusicChangeListener, OnStatusChangeListener, OnProgressListener, OnMusicClickListener {

    private ActivityMainBinding binding;
    private List<Music> musicList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        for (int i = 2; i < 4; i++) {
            Music music = new Music();
            music.pathUrl = "https://m2.czbtv.com/bucket-p/u-9bmym9uu142ws1sd/2020/09/27/02b4a2f7bb9a42b1a45822086d426f97_videogroup_480dc687048c447a9b8a2f8cf8419289_000.mp4";
            music.iamgePath = "http://czbtvoss.sobeylingyun.com/2020/9/18/4bfcd4e3-75e2-d818-e008-cc3d96fda5f5.jpeg?x-oss-process=style/thumbnail";
            music.singer = "标题" + (i + 1);
            music.title = "标题" + (i + 1);
            music.musicId = (i + 1) + "";

            musicList.add(music);
        }

        binding.btnInit.setOnClickListener(v -> {
            MusicPlayer.initialize(this);
            MusicPlayer.getInstance().registerBuffingListener(this);
            MusicPlayer.getInstance().registerMusicChangeListener(this);
            MusicPlayer.getInstance().registerStatusChangeListener(this);
            MusicPlayer.getInstance().registerProgressListener(this);
            MusicPlayer.getInstance().registerMusicClickListener(this);

        });

        binding.btnAdd.setOnClickListener(v -> {
            MusicPlayer.getInstance().setMusicList(musicList);
        });

        binding.btnPlay.setOnClickListener(v -> {
            if (!MusicPlayer.getInstance().requestNotification()) {
                MusicPlayer.getInstance().play();
                MusicPlayer.getInstance().showFloatWindow();
            }
        });
    }

    @Override
    protected void onDestroy() {
        MusicPlayer.getInstance().unregisterBuffingListener(this);
        MusicPlayer.getInstance().unregisterMusicChangeListener(this);
        MusicPlayer.getInstance().unregisterStatusChangeListener(this);
        MusicPlayer.getInstance().unregisterProgressListener(this);
        MusicPlayer.getInstance().unregisterMusicClickListener(this);
        super.onDestroy();
    }

    @Override
    public void onBufferingStart() {

    }

    @Override
    public void onBufferingEnd() {

    }

    @Override
    public void onMusicChange(int newPosition, int oldPosition) {

    }

    @Override
    public void onStatusChange(int status) {

    }

    @Override
    public void onProgress(long current, long duration, int bufferedPercentage) {
        Log.d("info","====播放进度===current=" + current + "===duration=" + duration);
    }

    @Override
    public void onMusicClick(Music music, int position) {

    }
}